﻿Ext.define('Html5App.controller.HomeController', {
	extend : 'Ext.app.Controller',
	config : {
		models : [],
		views : [],
		refs : {
			home : 'home',
			appExitAction : 'button[action=appExitAction]',
			downloadData : 'button[action=downloadData]',
			downloadFile : 'button[action=downloadFile]',
			updateAPP : 'button[action=updateAPP]',
			searchCustomer : 'button[action=searchCustomer]'
		},
		control : {
			home : {
				activate : function(newActiveItem, t, oldActiveItem, eOpts) {
					Ext.getCmp('username_label').setHtml(config.current_user.sales_name+"("+config.current_user.user_name+")");
				}
			},
			appExitAction : {
				tap : function(button, e, eOpts) {
					navigator.notification.confirm(
						'確定要退出嗎？',
						function(btnId){
							if(btnId==2){
								navigator.app.exitApp();
							}
				        }, '溫馨提示', ['取消','確定']
					);
				}
			},
			downloadData : {
				tap : function(button, e, eOpts) {
					navigator.notification.confirm(
						'同步數據和產品圖片等待時間較長，確定要同步嗎？',
						function(btnId){
							if(btnId==2){
			                	custom.downloadDataBySalesCode(config.current_user.user_name,false);
							}
				        }, '溫馨提示', ['取消','確定']
					);
				}
			},
			downloadFile : {
				tap : function(button, e, eOpts) {
					util.showLoadMask(null);
					if(navigator.localfile && typeof(navigator.localfile.downloadFile) != "undefined"){
			    		var resultCallback = function(result) {
			    			if(result.returnMap && result.returnMap !=""){
			    				var arrObj = result.returnMap.replace(/\{"/g, "").replace(/\"}/g, "").split('", "');
			    				config.database.transaction(
									function (tx) {
										//清空LocalFile
						                tx.executeSql("delete from LocalFile",
						                	[],
						                	function (tx, r) {},
						                	function (tx, error) {}
						                );
						                for(var obj in arrObj){
					    					var fileObj = arrObj[obj].split('"="');
					    					var filetype = "";
					    					var filename = fileObj[0];
					    					var filepath = fileObj[1];
					    					if(fileObj[1].indexOf("Doc/PDF") > -1){
					    						filetype = "PDF";
					    					}else if(fileObj[1].indexOf("Doc/Video") > -1){
					    						filetype = "Video";
					    					}else{
					    						filetype = "Picture";
					    					}
					    					//写入LocalFile表
	                        				tx.executeSql("insert into LocalFile(filetype,filename,filepath) " +
	                        							  "values ('"+filetype+"','"+filename+"','"+filepath+"')",
												[],
												function (tx, r) {},
												function (tx, e) {}
											);
					    				}
									}
								);
			    			}
			    			util.hideLoadMask();
			    		};
			    		setTimeout(function downloadFileFun(){
			    			navigator.localfile.downloadFile(resultCallback);
			    		},1500);
			    	} else {
			    		util.hideLoadMask();
			    	}
				}
			},
			updateAPP : {
				tap : function(button, e, eOpts) {
					window.updateapp.updateapp(null,null);
				}
			},
			searchCustomer : {
				tap : function(button, e, eOpts) {
					var searchKeyword = Ext.getCmp('searchKeyword').getValue();
					if(searchKeyword == ""){
						util.showMessageMask('請輸入客戶號碼/名稱','warn-max');
						return;
					}
					util.showLoadMask(null);
					config.database.transaction(
				        function (tx) {
				        	tx.executeSql(
				        		"select * from Customer where SalesCode='"+config.current_user.user_name+"' and (CustomerCode like '%"+searchKeyword+"%' or CustomerName like '%"+searchKeyword+"%')", 
				        		[],
				        		function (tx, r) {
				        			util.hideLoadMask();
				        			if(Number(r.rows.length) > 0){
				        				var obj = r.rows.item(0);
				        				config.customer_code = "customer_button_"+obj['CustomerCode']+"_"+obj['SalesCode'];
	                        			Ext.getCmp("titleBar").setActiveItem(1);
	                        			Ext.getCmp("customerHome").removeAll();
										Ext.getCmp("customerHome").add({
											xtype : 'customerInfo'
										});
										config.database.transaction(
											function (txc) {
												txc.executeSql(
										        		"select * from Area where CodeTerr='"+obj['CodeTerr']+"'", 
										        		[],
										        		function (tx, r) {
										        			if(Number(r.rows.length) > 0){
										        				var areaObj = r.rows.item(0);
										        				//清空LastSelectArea表
																txc.executeSql("delete from LastSelectArea",
																	[],
																	function (txc, r) {},
																	function (txc, e) {}
																);
									            				//写入LastSelectArea表
									            				txc.executeSql("insert into LastSelectArea(CodeTerr,CodeNameTerr) values ('"+areaObj['CodeTerr']+"','"+areaObj['CodeTerr']+' '+areaObj['NameTerr']+"')",
																	[],
																	function (txc, r) {
																		
																	},
																	function (txc, e) {}
																);
										        			}
										        		},
													function (txc, e) {}
												);
											}
										);
										config.database.transaction(
											function (txc) {
												//清空LastSelectCustomer表
												txc.executeSql("delete from LastSelectCustomer",
													[],
													function (txc, r) {},
													function (txc, e) {}
												);
		                        				//写入LastSelectCustomer表
		                        				txc.executeSql("insert into LastSelectCustomer(CustomerCode) values ('customer_button_"+obj['CustomerCode']+"_"+config.current_user.user_name+"')",
													[],
													function (txc, r) {
														
													},
													function (txc, e) {}
												);
											}
										);
				        			}else{
				        				util.showMessageMask('沒有找到相關客戶','warn-max');
				        			}
				        		},
				        		function (tx, e) {
				        			util.hideLoadMask();
				        		}
				            );
				        }
				    );
				}
			}
		}
	}
});